package com.changwu.cwbase.util;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.view.View;

import com.bigkoo.pickerview.TimePickerView;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.Utils;
import com.changwu.cwbase.App;
import com.changwu.cwbase.R;
import com.changwu.cwbase.bean.entity.ShareSDKState;
import com.changwu.cwbase.interfaces.AppShareListener;
import com.changwu.cwbase.interfaces.AppTimeListener;
import com.changwu.cwbase.widget.voice.MediaManager;
import com.lljjcoder.Interface.OnCityItemClickListener;
import com.lljjcoder.style.citypickerview.CityPickerView;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class AppOperateUtil {
    private static int themeId = R.style.picture_default_style;
    private static Context context = Utils.getApp();

    /**
     * 拨打电话（跳转到拨号界面，用户手动点击拨打）
     *
     * @param phoneNum 电话号码
     */
    public static void diallPhone(String phoneNum, Activity activity) {
        if (StringUtils.isTrimEmpty(phoneNum)) {
            ToastUtils.showShort("号码为空");
            return;
        }
        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri data = Uri.parse("tel:" + phoneNum);
        intent.setData(data);
        activity.startActivity(intent);
    }

    /**
     * 拨打电话（直接拨打电话）
     *
     * @param phoneNum 电话号码
     */
    public static void callPhoneImmediate(String phoneNum) {
        if (StringUtils.isTrimEmpty(phoneNum)) {
            ToastUtils.showShort("号码为空");
            return;
        }
        Intent intent = new Intent(Intent.ACTION_CALL);
        Uri data = Uri.parse("tel:" + phoneNum);
        intent.setData(data);
        if (ActivityCompat.checkSelfPermission(Utils.getApp(), Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        Utils.getApp().startActivity(intent);
    }

    public static void ImagePre(Activity activity, String path) {
        if (!StringUtils.isTrimEmpty(path)) {
            List<LocalMedia> lookList = new ArrayList<>();
            LocalMedia localMedia = new LocalMedia();
            localMedia.setPath(path);
            localMedia.setPictureType("image/jpg");
            lookList.add(localMedia);
            PictureSelector.create(activity).themeStyle(themeId).openExternalPreview(0, lookList);

        }
    }

    public static void preImage(Activity activity, List<String> paths) {
        List<LocalMedia> lookList = new ArrayList<>();
        LocalMedia localMedia = null;
        if (ListUtil.isNotEmpty(paths)) {
            for (int i = 0; i < paths.size(); i++) {
                localMedia = new LocalMedia();
                localMedia.setPath(paths.get(i));
                localMedia.setPictureType("image/jpg");
                lookList.add(localMedia);
            }
            PictureSelector.create(activity).themeStyle(themeId).openExternalPreview(0, lookList);

        }
    }

    public static void preVideo(Activity activity, String path) {
        if (!StringUtils.isTrimEmpty(path)) {
            PictureSelector.create(activity).externalPictureVideo(path);
        }
    }

    public static void preVoice(String path) {
        if (!StringUtils.isTrimEmpty(path)) {
            MediaManager.release();
            //开始实质播放
            MediaManager.playSound(path,
                    new MediaPlayer.OnCompletionListener() {
                        @Override
                        public void onCompletion(MediaPlayer mp) {
                        }
                    });
        }
    }

    public static void openCamera(Activity activity) {
        openCamera(activity, PictureConfig.CHOOSE_REQUEST);
    }

    public static void openCamera(Activity activity, int req) {
        PictureSelector.create(activity)
                .openGallery(PictureMimeType.ofImage())
                .maxSelectNum(1)
                .minimumCompressSize(100)
                .compress(true)
                .forResult(req);
    }

    public static void openVideo(Activity activity) {
        PictureSelector.create(activity)
                .openGallery(PictureMimeType.ofVideo())
                .maxSelectNum(1)
                .compress(true)
//                .videoMinSecond(3)
//                .videoMaxSecond(10)
                .videoQuality(0)
                .forResult(PictureConfig.CHOOSE_REQUEST);
    }

    private void playVoice(String path) {
        if (StringUtils.isTrimEmpty(path)) {
            ToastUtils.showShort("播放地址不能为空");
            return;
        }
        //播放前重置。
        MediaManager.release();
        //开始实质播放
        MediaManager.playSound(path,
                new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                    }
                });
    }

    public static void openCityPicker(OnCityItemClickListener listener) {
        CityPickerView mPicker = App.getInstance().getCityPicker();
        mPicker.setOnCityItemClickListener(listener);
        mPicker.showCityPicker();
    }

    public static void openTimePicker(AppTimeListener listener) {
        openTimePicker(TimePickerView.Type.YEAR_MONTH_DAY, listener);
    }

    private static void openTimePicker(TimePickerView.Type type, AppTimeListener listener) {
        openTimePicker(type, null, null, null, listener);
    }

    /**
     * yyyy-MM-dd
     */
    public static void openTimePicker(TimePickerView.Type type, Calendar startCalendar, Calendar endCalendar, Calendar selectCalendar, final AppTimeListener listener) {
        Calendar startDate = Calendar.getInstance();
        if (startCalendar == null) {
            startDate.set(1950, 0, 1, 0, 0, 0);
        } else {
            startDate = startCalendar;
        }
        Calendar endDate = Calendar.getInstance();
        if (endCalendar != null) {
            endDate = endCalendar;
        }
        Calendar selectedDate = Calendar.getInstance();
        if (selectCalendar != null) {
            selectedDate = selectCalendar;
        }
        //日期选择器
        TimePickerView pvTime = new TimePickerView.Builder(context, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                listener.onCallBack(date);
            }
        }).setType(TimePickerView.Type.YEAR_MONTH_DAY)
                .setDate(selectedDate)
                .setRangDate(startDate, endDate)
                .setTitleText("请选择日期")
                .build();
        pvTime.show();
    }

    public static void showShare(String title, String content, String link, String type, AppShareListener lexiwedCommonCallBack) {
        ShareSDKState shareInfo = new ShareSDKState();
        shareInfo.setTitle(title);
        shareInfo.setContent(content);
        shareInfo.setUrl(link);
        shareInfo.setShareType(type);
        final CustomerShare customerShare = new CustomerShare();
        customerShare.setContext(Utils.getApp());
        customerShare.setSds(shareInfo);
        customerShare.setUrl(shareInfo.getUrl());
        customerShare.setCommonListener(lexiwedCommonCallBack);
        customerShare.setType(shareInfo.getShareType());
        customerShare.shareUiState();
    }
}
